<?php

/**
 * 品牌控制器
 */
class BrandController extends AuthController {
	private $model;
	public function __init() {
		parent::__init();
		$this -> model = K('brand');

	}
	
	/**
	 * 删除一个品牌
	 */
	 public function delBrand(){
	 	$bid = Q('get.bid');
		if($this->model->delete($bid)){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	 }
	
	/**
	 * 更新一个品牌
	 */
	 public function editBrand(){
	 	$bid = Q('get.bid');
		if(IS_POST){
			//不存在上传文件的时候
			if(!isset($_FILES['blogo'])){
				//将旧LOGO图片赋值给原来的字段
				$_POST['blogo'] = Q('post.cblogo');
				if($this->model->updateBrand()){
					$this->success('更新成功',U('Brand/Index'));
				}else{
					$this->success($this->model->error);
				}
			}else{
				//如果 上传 的文件为空那么将原来的文件赋值给blogo这样就不会修改原来内容
				if($_FILES['blogo']['error']==4){
					$_POST['blogo'] = Q('post.cblogo');
				}
				//如果上传的错误 不为4那么表示有上传文件那么将他获得到替换原来的文件
				$upSavePath = C('UPLOAD_PATH') . 'BrandLogo';
				//将logo图片上传
				$upload = new Upload($upSavePath);
				$upfile = current($upload -> upload('blogo'));
				//缩略图
				$img = new Image();
				$thumbFile = $img -> thumb($upfile['path'], null, Q('post.width'), Q('post.height'), 6);
				if (!$thumbFile) {

				} else {
					Q('post.blogo', $thumbFile);
					//写入文件
					if ($this -> model -> save()) {
					//删除上传的原文件
					unlink($upfile['path']);
					//删除原来的文件
					unlink(Q('post.cblogo'));
					$this -> success('更新品牌成功',U('Brand/Index'));

					} else {
					$this -> error($this -> model -> error);
				}

			}
				
				
				
			}
		}
		$this->assign('brand',$this->model->where("bid={$bid}")->find());
		$this->display();
		
	 }
	
	
	
	/**
	 * 显示所有品牌列表
	 */
	public function index() {
		$page = new Page($this->model->count(),10,10);
		$this->assign('page',$page->show(2));
		$this->assign('brands',$this->model->order("bid desc")->limit($page->limit())->all());
		$this -> display();
	}
	
	/**
	 * 更新检查
	 */
	 public function updateChcekBrandName(){
	 	
	 	$brandName = Q('post.bname');
		$bid = Q('post.bid');
		//获得原来 的品牌名
		$oldBname = $this->model->where("bid={$bid}")->find()['bname'];
		//查询新的品牌名是否存在
		if ($this -> model -> where("bname not in ('$oldBname') AND bname='{$brandName}'")->find()) {
			$this -> ajax(array('message' => '品牌已经存在请更换', 'status' => 0));
		} else {
			$this -> ajax(array('message' => '品牌可以添加', 'status' => 1));
		}
	 }
	

	/**
	 * 添加品牌方法
	 */
	public function addBrand() {

		if (IS_POST) {
			//先处理上传的图片将他压到
			//保存的目录
			$upSavePath = C('UPLOAD_PATH') . 'BrandLogo';
			if ($_FILES['blogo']['error'] == 4) {
				$this -> error('logo上传失败,可能是文件不支持或文件过大');
			}
			//将logo图片上传
			$upload = new Upload($upSavePath);
			$upfile = current($upload -> upload('blogo'));
			//缩略图
			$img = new Image();
			$thumbFile = $img -> thumb($upfile['path'], null, Q('post.width'), Q('post.height'), 6);
			if (!$thumbFile) {

			} else {
				Q('post.blogo', $thumbFile);
				//写入文件
				if ($this -> model -> addBrand()) {
					//删除原文件
					unlink($upfile['path']);
					$this -> success('添加品牌成功',U('Brand/Index'));

				} else {
					$this -> error($this -> model -> error);
				}

			}

		}

		$this -> display();
	}

	/**
	 * 检测品牌是否存在
	 */
	public function checkBrandName() {
		$brandName = Q('post.bname');
		if ($this -> model -> checkBrandName($brandName)) {
			$this -> ajax(array('message' => '品牌已经存在请更换', 'status' => 0));
		} else {
			$this -> ajax(array('message' => '品牌可以添加', 'status' => 1));
		}
	}

}
